﻿using System;
using System.Collections.Generic;
using ParserGenerator.Collections;

namespace ParserGenerator.ParserConstruction
{
    [Serializable]
    public sealed class ParserAutomatonAnnotationNodes : ReadOnlyLinkedList<ParserAutomatonAnnotationNode>
    {
        internal ParserAutomatonAnnotationNodes()
            : base(new LinkedList<ParserAutomatonAnnotationNode>())
        {
        }

        internal bool TryInsert(ParserAutomatonAnnotationNode node)
        {
            if (node == null)
                throw new ArgumentNullException(nameof(node));
            for (LinkedListNode<ParserAutomatonAnnotationNode> p = Items.First; p != null; p = p.Next)
            {
                int t = node.CompareTo(p.Value);
                if (t < 0)
                {
                    Items.AddBefore(p, node);
                    return true;
                }
                else if (t == 0)
                {
                    return false;
                }
            }
            Items.AddLast(node);
            return true;
        }
    }
}
